﻿function AutomateWPS(tableid,letter,title,unit,num)
{
    try
    {
        //        var curTbl = document.getElementById(tableid);
        var oXL = new ActiveXObject("ET.Application"); 
        if (oXL == null)
        {
            alert('导出WPS失败，确定是否安装了WPS，浏览器设置是否正确');
        }else{
            //创建AX对象excel 
            var oWB = oXL.Workbooks.Add(); 
            //获取workbook对象 
            var oSheet = oWB.ActiveSheet; 
            //激活当前sheet 
            var Lenr =tableid.rows.length;// curTbl.rows.length;
            //取得表格行数 
            var merge='';
            merge ='A1:'+letter+'1';
            oXL.Range(merge).MergeCells = true;  //合并单元格
            merge ='A2:'+letter+'2';
            oXL.Range(merge).MergeCells = true;  //合并单元格

            oSheet.Cells(1,1).HorizontalAlignment = 3;            
            oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
            oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
            
            if(title=='峰谷比统计'){      
                if(unit==1){
                    merge ='A3:A5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='B3:B5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='C3:C5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='D3:D5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='E3:E5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='F3:F5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='G3:G5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    
                    merge ='H3:M3';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='H4:I4';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    
                    merge ='N3:S3';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='N4:O4';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    
                    merge ='T3:U3';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='T4:U4';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    
                    oSheet.Columns("I:M").ColumnWidth = 18;  //设置单元格宽度
                    oSheet.Columns("O:U").ColumnWidth = 18;  //设置单元格宽度
                    
                    for(k=2;k<22;k++){                
                        oSheet.Cells(3,k).HorizontalAlignment = 3; 
                        oSheet.Cells(4,k).HorizontalAlignment = 3; 
                        oSheet.Cells(5,k).HorizontalAlignment = 3;     
                    }
                    
                    for(k=2;k<num+7;k++){                  
                        oSheet.Cells(k,2).HorizontalAlignment = 4;
                        oSheet.Cells(k,3).HorizontalAlignment = 4;    
                    }
                   
                    for (i = 0; i < Lenr; i++) 
                    { 
                        var Lenc = tableid.rows(i).cells.length; 
                        //取得每行的列数 
                        for (j = 0; j < Lenc; j++) 
                        {
                            var cellValue;
                            cellValue = tableid.rows(i).cells(j).innerText;
                            if(j==1 || j==2)
                            {
                                cellValue = "'"+cellValue; //转换为文本格式
                            } 
                            if(i==2){
                                if(j==8)
                                    oSheet.Cells(i + 1, j + 6).value = cellValue; 
                                if(j==9)
                                    oSheet.Cells(i + 1, j + 11).value = cellValue; 
                                else
                                    oSheet.Cells(i + 1, j + 1).value = cellValue; 
                                 
                            }else if(i==3)
                            {
                                if(j==0)
                                    oSheet.Cells(i + 1, j + 8).value = cellValue;  
                                else if(j>5)
                                    oSheet.Cells(i + 1, j + 10).value = cellValue;  
                                else
                                    oSheet.Cells(i + 1, j + 9).value = cellValue;   
                            }else if(i==4)
                            {                                
                                oSheet.Cells(i + 1, j + 8).value = cellValue;
                            }else{
                                oSheet.Cells(i + 1, j + 1).value = cellValue;   
                            }
                        } 
                    }                 
                }else if(unit==2){
                    merge ='A3:A5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='B3:B5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='C3:C5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='D3:D5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='E3:E5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='F3:F5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    merge ='G3:G5';
                    oXL.Range(merge).MergeCells = true;  //合并单元格 
                    
                    merge ='H3:M3';
                    oXL.Range(merge).MergeCells = true;  //合并单元格                    
                    merge ='H4:I4';
                    oXL.Range(merge).MergeCells = true;  //合并单元格  
                    
                    merge ='N3:O3';
                    oXL.Range(merge).MergeCells = true;  //合并单元格      
                    merge ='N4:O4';
                    oXL.Range(merge).MergeCells = true;  //合并单元格  
                    
                    oSheet.Columns("G:Q").ColumnWidth = 18;  //设置单元格宽度
                    
                    for(k=2;k<15;k++){                
                        oSheet.Cells(3,k).HorizontalAlignment = 3; 
                        oSheet.Cells(4,k).HorizontalAlignment = 3; 
                        oSheet.Cells(5,k).HorizontalAlignment = 3;     
                    }
                    
                    for(k=2;k<num+7;k++){                  
                        oSheet.Cells(k,2).HorizontalAlignment = 4;
                        oSheet.Cells(k,3).HorizontalAlignment = 4;    
                    }
                    
                    for (i = 0; i < Lenr; i++) 
                    { 
                        var Lenc = tableid.rows(i).cells.length; 
                        //取得每行的列数 
                        for (j = 0; j < Lenc; j++) 
                        {
                            var cellValue;
                            cellValue = tableid.rows(i).cells(j).innerText;
                            if(j==1 || j==2)
                            {
                                cellValue = "'"+cellValue; //转换为文本格式
                            } 
                            if(i==2){
                                if(j==8)
                                    oSheet.Cells(i + 1, j + 6).value = cellValue; 
                                else
                                    oSheet.Cells(i + 1, j + 1).value = cellValue; 
                            }else if(i==3)
                            {
                                if(j==0)
                                    oSheet.Cells(i + 1, j + 8).value = cellValue;  
                                else if(j>5)
                                    oSheet.Cells(i + 1, j + 10).value = cellValue;  
                                else
                                    oSheet.Cells(i + 1, j + 9).value = cellValue;   
                            }else if(i==4)
                            {
                                oSheet.Cells(i + 1, j + 8).value = cellValue;
                            }else{
                                oSheet.Cells(i + 1, j + 1).value = cellValue;   
                            }  
                        } 
                    } 
                }else if(unit==0){
                                   
                    oSheet.Columns("B:H").ColumnWidth = 15;  //设置单元格宽度
                       
                    for(k=2;k<7;k++){                
                        oSheet.Cells(3,k).HorizontalAlignment = 3;     
                    }
                    
                    for(k=2;k<num+2;k++){                  
                        oSheet.Cells(k,2).HorizontalAlignment = 4;
                        oSheet.Cells(k,3).HorizontalAlignment = 4;    
                    }
                    
                    for (i = 0; i < Lenr; i++) 
                    { 
                        var Lenc = tableid.rows(i).cells.length; 
                        //取得每行的列数 
                        for (j = 0; j < Lenc; j++) 
                        {
                            var cellValue;
                            cellValue = tableid.rows(i).cells(j).innerText;
                            if(j==1 || j==2)
                            {
                                cellValue = "'"+cellValue; //转换为文本格式
                            }  
                            oSheet.Cells(i + 1, j + 1).value = cellValue; 
                        } 
                    } 
                }                             
            }else{
                for (i = 0; i < Lenr; i++) 
                { 
                    var Lenc = tableid.rows(i).cells.length; 
                    //取得每行的列数 
                    for (j = 0; j < Lenc; j++) 
                    {
                        var cellValue;
                        cellValue = tableid.rows(i).cells(j).innerText;
                        if(j==1 || j==2)
                        {
                            cellValue = "'"+cellValue; //转换为文本格式
                        }  
                        oSheet.Cells(i + 1, j + 1).value = cellValue; 
                    } 
                } 
            }                    

            //设置excel可见属性
            oXL.Visible = true;
        }
    }catch(e){
        alert("导出WPS失败，确定是否安装了WPS");

        oXL = null; 
        oWB = null; 
        oSheet = null;
    }
    //oSheet.Columns.AutoFit; 
    //oXL.UserControl = true;  //wps交由用户控制 很重要
}